#!/bin/sh
# by hugo

LOGTIME=$(date "+%Y-%m-%d %H:%M:%S")
echo '['$LOGTIME'] Chnroute IP list update now.'
echo downloading Chnroute IP ......
wget -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > /tmp/china_ssr.txt
echo deal the chnroute IP list......
icount=$(cat /tmp/china_ssr.txt | wc -l)
oldcount=$(cat /etc/china_ssr.txt | wc -l)
echo old Chnroute IP list count: $oldcount
echo new Chnroute IP list count: $icount
if [ $icount -eq 0 ];then
	echo "The new Chnroute IP list is error! Chnroute IP list No Change!"
elif [ $icount -ne $oldcount ]; then
	cp -f /tmp/china_ssr.txt /etc/china_ssr.txt
	echo "Update Chnroute IP List Done! Now restart ShadowsocksR, please wait......"
	/etc/init.d/shadowsocksr restart
	if ps -w | grep chinadns | grep -v grep >/dev/null 2>&1; then
		echo "Now restart ChinaDNS......"
		/etc/init.d/chinadns restart
	    fi
else
	echo "Chnroute IP list No Change!"
fi
rm -f /tmp/china_ssr.txt
echo "Restart complete! Enjoy it!"
